package com.igeek.compus.test.user.controller;

import cn.hutool.core.util.StrUtil;
import com.igeek.compus.test.menu.entity.Menu;
import com.igeek.compus.test.system.Constants;
import com.igeek.compus.test.user.entity.User;
import com.igeek.compus.test.user.service.UserService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController{

    @Autowired
    private UserService userService;

    @RequestMapping("list.do")
    @ResponseBody
    public Map list(User user) {

        System.out.println(user);

        Map<String, Object> result = new HashMap<>();

        try {
            // 业务代码，用户信息查询
            List<User> userList = userService.getUserListByPage(user);
            int count = userService.getUserListCount(user);

            result.put("data", userList);
            result.put("count", count);

            result.put("code", 0);
            result.put("msg", "查询成功");
        }catch (Exception e) {
            e.printStackTrace();
            result.put("code", 1);
            result.put("msg", "系统异常");
        }
        return result;
    }

    @RequestMapping("deleteUserById.do")
    @ResponseBody
    public void deleteUserById(User user) {
        System.out.println(user);
        userService.deleteUserById(user);
    }

    @RequestMapping("deleteUserList.do")
    @ResponseBody
    public Map<String, Object> deleteUserList(String userIds) {
        Map<String, Object> result = new HashMap<>();

        List<String> userIdList = Arrays.asList(userIds.substring(1, userIds.length() - 1).split(","));

        // 2.执行业务
        try {
            if (userService.deleteUserList(userIdList) > 0) {
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            } else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }

        // 3.返回结果
        return result;
    }

    @RequestMapping("addUser.do")
    @ResponseBody
    public Map<String, Object> addUser(User user) {
        Map<String, Object> result = new HashMap<>();

        // 1.参数校验
        if(StrUtil.isEmpty(user.getNickName()) || StrUtil.isEmpty(user.getPhone())
                || user.getAge() == null) {
            result.put(Constants.RETCODE, Constants.ERROR_CODE);
            result.put(Constants.RETMSG, Constants.PARAM_ERROR_MSG);
            return  result;
        }

        // 2.执行业务
        try {
            if(userService.addUser(user) > 0) {
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }
        }catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }

        // 3.返回结果
        return result;
    }

    @RequestMapping("toUserEdit.do")
    public ModelAndView toUserEdit(Integer id) {
        // 通过id查询menu信息
        User user = userService.getUserInfoById(id);

        Map<String, Object> map = new HashMap<>();
        map.put("id", user.getId());
        map.put("nickName", user.getNickName());
        map.put("phone", user.getPhone());
        map.put("sex", user.getSex());
        map.put("age", user.getAge());
        map.put("email", user.getEmail());

        return new ModelAndView("/user/user_edit", map);
    }

    @RequestMapping("updateUser.do")
    @ResponseBody
    public Map<String, Object> updateUser(User user) {
        Map<String, Object> result = new HashMap<>();

        // 1.参数校验
        if(user.getId() == null) {
            result.put(Constants.RETCODE, Constants.ERROR_CODE);
            result.put(Constants.RETMSG, Constants.PARAM_ERROR_MSG);
            return  result;
        }

        // 2.执行业务
        try {
            if(userService.updateUser(user) > 0) {
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }
        }catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }

        // 3.返回结果
        return result;
    }

    @RequestMapping("exportUserList.do")
    @ResponseBody
    public void export(HttpServletResponse response) throws Exception {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet sheet = hssfWorkbook.createSheet();
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("通知名称");

        response.setHeader("content-disposition", "attachment;filename=menuList.xls");
        OutputStream outputStream = response.getOutputStream();
        hssfWorkbook.write(outputStream);

        outputStream.flush();
        outputStream.close();

    }

}
